<template>
    <div class="search">
        <teleport to=".header">
            <van-nav-bar class="navBar" left-arrow @click-left="back">
                <template #title>
                    <form action="/">
                        <van-search v-model="value" show-action placeholder="请输入搜索关键词" @search="onSearch"
                            @cancel="onCancel" />
                    </form>
                </template>
            </van-nav-bar>
        </teleport>

        <div class="dataInfo" v-if="isShow" v-for="(item, index) in list" :key="index">
            <div class="upPart">
                <div class="title">
                    <p>{{ item.address }}</p>
                </div>
                <div class="tags">
                    <span v-for="(i, v) in item.tags"><van-tag plain type="primary">{{ i.label }}</van-tag></span>
                </div>
                <div class="address"><van-icon name="guide-o" />
                    <p>金融港</p>
                </div>
                <div class="time">
                    <span>{{ item.time }}</span>
                    <span><van-icon name="hot-o" />热度：{{ item.hot }}</span>
                </div>
            </div>
            <div class="imgPart">
                <img :src="'http://81.69.235.62:3000/' + item.imgs[0].url.slice(7)" alt="">
                <!-- {{item.imgs[0].url.slice(6) }} -->
            </div>
            <div class="content">
                <p>{{ item.content }}</p>
            </div>
            <hr>
            <div class="like">
                <div><van-icon name="good-job-o">1</van-icon></div>
                <div><van-icon name="edit">2</van-icon></div>
                <div><van-icon name="like-o">3</van-icon></div>
            </div>
        </div>
        <div class="cancelView" v-else>
            <van-empty image="https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg" image-size="80" description="请先搜索嗷" />
            <p>关键词 为旅游标题和分类</p>

            <div class="history">
                <div>
                    <h1>搜索记录</h1>
                </div>
                <div v-for="(val, index) in new Set(searchList)" class="cell" :key="index">
                    <p>{{ val }}</p>
                </div>
            </div>
        </div>


    </div>
</template>

<script setup>

import { ref, onMounted } from 'vue';
import { showToast } from 'vant';
import { getAllTravelsAPI } from '../../api/search';
import _ from 'lodash'

const value = ref('');
const allList = ref({})
const list = ref({})
const isShow = ref(false)
const searchList = ref([])
// console.log(searchList.value.concat(JSON.parse(localStorage.getItem("searchHistory"))));
// console.log(localStorage.getItem("searchHistory"));
// console.log(searchList.value);

const getAllTravels = async () => {
    let res = await getAllTravelsAPI();
    allList.value = res.result
    console.log(allList);
}

const onSearch = (value) => {
    isShow.value = true;
    searchList.value.push(value)
    localStorage.setItem("searchHistory",JSON.stringify(searchList.value))
    console.log(value);
    list.value = _.filter(allList.value, (item) => (item.title.indexOf(value) > -1) || (_.some(item.tags, (v) => v.label == value)))
    // list.value = Array.from(allList.value).filter((item) => item.title.indexOf(value) > -1) || (item.tags.some((v) => v.label == value))
    console.log(list);
}
const onCancel = () => {
    isShow.value = false
}




onMounted(() => {
    getAllTravels();
    
})

</script>

<style lang="scss" scoped>
.search {
    width: 93%;
    padding: 0.15rem;

    .dataInfo {
        width: 100%;
        margin: auto;
        border: 1px solid Lightgray;
        padding: 0.1rem;
        border-radius: .20rem;
        margin-bottom: 0.3rem;

        .upPart {
            line-height: 1.5;

            .tags span {
                margin-right: 10px;
            }

            .address {
                font-size: .30rem;

                p {
                    display: inline-block;
                }

            }

            .time {
                display: flex;
                justify-content: space-between;
                font-size: .20rem;
                color: gray
            }
        }

        .imgPart {
            width: 100%;
            // padding: 10px;

            img {
                width: 100%;
                border-radius: .20rem;
            }

        }

        .content {
            line-height: 1.5;
            font-size: 0.3rem;
            color: gray;
        }

        .like {
            display: flex;
            justify-content: space-around;

            div {
                text-align: center;

            }
        }

    }

    .cancelView {
        width: 100%;

        p:nth-of-type(1) {
            text-align: center;
            font-size: 0.3rem;
            color: gray;
        }

        .history {
            margin-top:0.5rem ;
            padding: 10px;

            h1 {
                font-weight: 600;
                line-height: 2;
                border-bottom: 1px solid lightgray;
            }
            .cell {
                line-height: 2;
                border-bottom: 1px solid lightgray;

                 p{
                    text-align: left;
                    text-indent: 2;
                }
            }
        }
    }
}
</style> 